package com.cy.pj.sys.web.advice;

import com.cy.pj.sys.web.pojo.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

@Slf4j //日志
@ControllerAdvice //=@ControllerAdvice+@RespnseBody
public class GlobalExcepctionHandler {

    @ExceptionHandler(NumberFormatException.class)
    public JsonResult doHandleNumberFormatException(NumberFormatException e){
        e.printStackTrace();//打印异常
        log.error("exception {}","你传入的参数值不存在");
//        return new JsonResult(e);
        return new JsonResult(0,"你传入的参数值不存在");
//        这种需要无参构造
//        JsonResult j =new JsonResult();
//        j.setState(0);
//        j.setMessage("你传入的参数值不存在");
//        return  j;
    }

//    一般写一样，要么是下面是注解的父类类型
    @ExceptionHandler(RuntimeException.class)
    public JsonResult doHandleRuntimeException(RuntimeException e){
        e.printStackTrace();//打印异常
        log.error("exception {}",e.getMessage());
        return new JsonResult(e);
    }
}
